import { get, post, put, del } from '@/utils/request'

export interface Deed {
  id: string
  time: string
  type: 'good' | 'bad'
  description: string
  merit: number
}

export interface Judgment {
  id: string
  time: string
  judge: string
  type: 'heaven' | 'hell' | 'reincarnate'
  duration: number
  remarks: string
}

export interface Reincarnation {
  id: string
  time: string
  officer: string
  location: string
  newIdentity: string
  remarks: string
}

export interface Soul {
  id: string
  name: string
  gender: 'male' | 'female'
  age: number
  birthDate: string
  deathDate: string
  lifespan: number
  birthplace: string
  status: 'pending' | 'judged' | 'punishing' | 'reincarnated'
  deeds: Deed[]
  judgments: Judgment[]
  reincarnations: Reincarnation[]
}

export interface SoulParams {
  name?: string
  status?: string
  startTime?: string
  endTime?: string
  page: number
  pageSize: number
}

export interface CreateSoulParams {
  name: string
  gender: 'male' | 'female'
  birthDate: string
  deathDate: string
  birthplace: string
}

export interface UpdateSoulParams {
  name?: string
  gender?: 'male' | 'female'
  birthDate?: string
  deathDate?: string
  birthplace?: string
  status?: 'pending' | 'judged' | 'punishing' | 'reincarnated'
}

const SoulAPI = {
  list: '/api/souls',
  create: '/api/souls',
  get: (id: string) => `/api/souls/${id}`,
  update: (id: string) => `/api/souls/${id}`,
  delete: (id: string) => `/api/souls/${id}`,
  deeds: {
    list: (id: string) => `/api/souls/${id}/deeds`,
    create: (id: string) => `/api/souls/${id}/deeds`,
    delete: (id: string, deedId: string) => `/api/souls/${id}/deeds/${deedId}`,
  },
  judgments: {
    list: (id: string) => `/api/souls/${id}/judgments`,
    create: (id: string) => `/api/souls/${id}/judgments`,
    delete: (id: string, judgmentId: string) => `/api/souls/${id}/judgments/${judgmentId}`,
  },
  reincarnations: {
    list: (id: string) => `/api/souls/${id}/reincarnations`,
    create: (id: string) => `/api/souls/${id}/reincarnations`,
    delete: (id: string, reincarnationId: string) => `/api/souls/${id}/reincarnations/${reincarnationId}`,
  },
}

export const SoulService = {
  async getSouls(params: SoulParams) {
    return get<{ total: number; items: Soul[] }>(SoulAPI.list, params)
  },

  async getSoul(id: string) {
    return get<Soul>(SoulAPI.get(id))
  },

  async createSoul(params: CreateSoulParams) {
    return post<Soul>(SoulAPI.create, params)
  },

  async updateSoul(id: string, params: UpdateSoulParams) {
    return put<Soul>(SoulAPI.update(id), params)
  },

  async deleteSoul(id: string) {
    return del(SoulAPI.delete(id))
  },

  async createDeed(id: string, deed: Omit<Deed, 'id'>) {
    return post<Deed>(SoulAPI.deeds.create(id), deed)
  },

  async deleteDeed(id: string, deedId: string) {
    return del(SoulAPI.deeds.delete(id, deedId))
  },

  async createJudgment(id: string, judgment: Omit<Judgment, 'id'>) {
    return post<Judgment>(SoulAPI.judgments.create(id), judgment)
  },

  async deleteJudgment(id: string, judgmentId: string) {
    return del(SoulAPI.judgments.delete(id, judgmentId))
  },

  async createReincarnation(id: string, reincarnation: Omit<Reincarnation, 'id'>) {
    return post<Reincarnation>(SoulAPI.reincarnations.create(id), reincarnation)
  },

  async deleteReincarnation(id: string, reincarnationId: string) {
    return del(SoulAPI.reincarnations.delete(id, reincarnationId))
  },
}
